﻿
Imports std

Namespace storageJobConsumerInternal
    Public MustInherit Class memslice
        Implements isyncslice

        Private _maxKeyCount As Int64 = 0
        Private _maxMemoryUsage As Int64 = 0

        Protected Function maxKeyCount() As Int64
            Return _maxKeyCount
        End Function

        Protected Function maxMemoryUsage() As Int64
            Return _maxMemoryUsage
        End Function

        Public Function capacity(ByRef c As Int64) As Boolean Implements isyncslice.capacity
            Dim i As Int64 = 0
            If valuesize(i) Then
                If i >= 0 Then
                    c = maxMemoryUsage() - i
                Else
                    c = npos
                End If
                Return True
            Else
                Return False
            End If
        End Function

        Public Function full(ByRef r As Boolean) As Boolean Implements isyncslice.full
            Dim i As Int64 = 0
            Dim j As Int64 = 0
            If keycount(i) AndAlso valuesize(j) Then
                r = (i >= maxKeyCount() OrElse j >= maxMemoryUsage())
                Return True
            Else
                Return False
            End If
        End Function

        Public Function empty(ByRef r As Boolean) As Boolean Implements isyncslice.empty
            Dim i As Int64 = 0
            If keycount(i) Then
                r = (i <= 0)
                Return True
            Else
                Return False
            End If
        End Function

        Protected Shared Function initial(ByVal name As String,
                                          ByVal config As configParser,
                                          ByVal c As memslice) As Boolean
            assert(Not c Is Nothing)
            assert(Not config Is Nothing)
            c._maxKeyCount = config.parameter(name, "maxKeyCount", 8388608)       '8M
            c._maxMemoryUsage = config.parameter(name, "maxMemoryUsage", 8388608) '8M
            Return True
        End Function

        Protected MustOverride Function delete(ByVal key As String) As Boolean
        Public Function delete(ByVal key As String,
                               ByRef result As Boolean) As Boolean Implements isyncslice.delete
            Return eva(result, delete(key))
        End Function

        Public Function heartbeat(ByRef result As Boolean) As Boolean Implements isyncslice.heartbeat
            assert(False)
            result = False
            Return False
        End Function

        Protected MustOverride Function keycount() As Int64
        Public Function keycount(ByRef result As Int64) As Boolean Implements isyncslice.keycount
            Return eva(result, keycount())
        End Function

        Protected MustOverride Function list() As vector(Of String)
        Public Function list(ByRef result As vector(Of String)) As Boolean Implements isyncslice.list
            Return eva(result, list())
        End Function

        Protected MustOverride Function modify(ByVal key As String, ByVal buff As bytes) As Boolean
        Public Function modify(ByVal key As String,
                               ByVal buff As bytes,
                               ByRef result As Boolean) As Boolean Implements isyncslice.modify
            Return eva(result, modify(key, buff))
        End Function

        Protected MustOverride Function read(ByVal key As String) As bytes
        Public Function read(ByVal key As String,
                             ByRef result As bytes) As Boolean Implements isyncslice.read
            Return eva(result, read(key))
        End Function

        Protected MustOverride Function retire() As Boolean
        Public Function retire(ByRef result As Boolean) As Boolean Implements isyncslice.retire
            Return eva(result, retire())
        End Function

        Protected MustOverride Function seek(ByVal key As String) As Boolean
        Public Function seek(ByVal key As String,
                             ByRef result As Boolean) As Boolean Implements isyncslice.seek
            Return eva(result, seek(key))
        End Function

        Protected MustOverride Function sizeof(ByVal key As String) As Int64
        Public Function sizeof(ByVal key As String,
                               ByRef result As Int64) As Boolean Implements isyncslice.sizeof
            Return eva(result, sizeof(key))
        End Function

        Public Overridable Sub [stop]() Implements isyncslice.stop
        End Sub

        Protected MustOverride Function valuesize() As Int64
        Public Function valuesize(ByRef result As Int64) As Boolean Implements isyncslice.valuesize
            Return eva(result, valuesize())
        End Function

        Protected MustOverride Function write(ByVal key As String, ByVal buff As bytes) As Boolean
        Public Function write(ByVal key As String,
                              ByVal buff As bytes,
                              ByRef result As Boolean) As Boolean Implements isyncslice.write
            Return eva(result, write(key, buff))
        End Function
    End Class
End Namespace
